//
//  ViewController.swift
//  ThemeManagement
//
//  Created by fanxiaoxin_1987@126.com on 06/20/2022.
//  Copyright (c) 2022 fanxiaoxin_1987@126.com. All rights reserved.
//

import UIKit
import ThemeManagement
import UICreate

extension TMTheme {
    /// 浅色系
    static var light: Self { return .name("light") }
    /// 深色系
    static var dark: Self { return .name("dark") }
}

extension TMValue where Value == UIColor {
    /// 背景色
    static var background: Self = [.default: 0xffffff, .dark: 0x000000]
    /// 背景色
    static var subBackground: Self = [.default: 0xdddddd]
    /// 文字色
    static var text: Self = [.light: 0x333333, .dark: 0xffffff]
    /// 边框色
    static var border: Self = [.light: 0xff0000, .dark: 0x00ff00]
}
extension TMValue where Value == UIStatusBarStyle {
    static var `default`: Self = .init([.dark: .lightContent])
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.theme.backgroundColor = .background
        
        let light = UIButton(.text("Light"), .event(self, #selector(self.toLight)))
        let dark = UIButton(.text("Dark"), .event(self, #selector(self.toDark)))
        light.theme.setTitleColor(.text, for: .normal)
        light.layer.borderWidth = 1
        light.layer.theme.borderColor = .border(light: .blue)
        light.theme.backgroundColor = .subBackground
        dark.layer.borderWidth = 1
        dark.theme.load { btn in
            btn.set(.border(.theme(.border)),
                    .bg(.theme(.subBackground(light: .green))),
                    .color(.theme(.text(dark: .red))))
        }
        self.view.build {
            vstack(.distribution(.fillEqually), .axis(.horizontal), .spacing(50)) {
                light
                dark
            }.layout(.top(50), .marginX(80), .height(44))
        }
        self.theme.load { _ in
            print("load")
        }
        self.theme.reload { vc in
            vc.setNeedsStatusBarAppearanceUpdate()
            print("reload")
        }
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .theme(.default) ?? .default
    }
    
    @objc func toLight() {
        TMThemeManager.shared.current = .light
    }
    @objc func toDark() {
        TMThemeManager.shared.current = .dark
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

